<?php
    class Event extends CActiveRecord
    {
        public static function model($className = __CLASS__) {
            return parent::model ( $className );
        }

        public function tableName() {                  
            return 'c_event';
        }

        public function getEventActive($currentPage, $numberRecordPerPage)
        {
            $cacheService = new CacheService("Event","getEventActive",$currentPage.$numberRecordPerPage);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $startRecord = ($currentPage - 1) * $numberRecordPerPage;  
                $sql = "SELECT * FROM c_event WHERE  status=1  LIMIT " . $startRecord . ", " . $numberRecordPerPage;
                $command = Yii::app()->db->createCommand($sql);
                $rows = $command->queryAll();  
                Yii::app()->cache->set($key, $rows, ConstantsUtil::TIME_CACHE_3600); 
            } else{
                $rows=$cache; 
            }
            return $rows;

        }
        public function getTotalEvent()
        {
            $sql = "SELECT count(id) FROM c_event WHERE  status=1 ";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryRow();        
            return $rows["count(id)"];
        }
        public function getInfoEvent($id)
        {
            $cacheService = new CacheService("Event","getInfoEvent",$id);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $connect =Yii::app()->db;
                $sql="SELECT * FROM c_event WHERE id=".$id." AND status=1";
                $command=$connect->createCommand($sql);
                $row = $command->queryRow();
                Yii::app()->cache->set($key, $row, ConstantsUtil::TIME_CACHE_3600);
            }
            else
            {
                $row=$cache;
            }
            return $row;
        }
        public function getRandomEvent(){
            $connect =Yii::app()->db;
            $sql="SELECT * FROM c_event WHERE status=1 ORDER BY RAND() LIMIT 0,5";
            $command=$connect->createCommand($sql);
            $rows= $command->queryAll();
            return $rows;
        }
    } 
?>
